<jsp:directive.page import="org.biojava.dasobert.dasregistry.DasSource"/>
<jsp:directive.page import="org.biojava.dasobert.dasregistry.Das1Source"/>
<jsp:directive.page import="org.biojava.dasobert.dasregistry.DasCoordinateSystem"/>
<jsp:directive.page import="dasregistry.dasClientLink"/>
<jsp:directive.page import="dasregistry.ActivationURLCreator" />
<jsp:directive.page import="org.biojava.services.das.registry.DasClient"/>
<jsp:directive.page import="org.biojava.services.das.registry.DasRegistrySql"/>
<jsp:directive.page import="org.biojava.services.das.registry.AutoActivation" />
<jsp:directive.page import="org.springframework.beans.factory.BeanFactory"/>
<jsp:directive.page import="org.springframework.context.support.ClassPathXmlApplicationContext"/>

<jsp:useBean id="register" class="dasregistry.registryBean" scope="session"/> 
<jsp:useBean id="bean"     class="dasregistry.updateBean"   scope="session"/>
<jsp:useBean id="coord"    class="dasregistry.coordBean"    scope="session"/>

<%@ page import="org.biojava.services.das.dao.RegistryUser" %>
<%@ page import="org.biojava.services.das.registry.UserManager" %>
<%@ page import="org.biojava.services.das.dao.Role" %>
<%@ page import="org.biojava.services.das.dao.RegistryUser" %>
<%@ page import="org.biojava.services.das.dao.ProjectBean"%>


<%@ include file="logincheck.jsp" %>

<% String title="edit AutoActivation URLS for server";
   String header="edit auto activation URLs";
   
    BeanFactory ctx=(new ClassPathXmlApplicationContext("SpringDAO.xml"));
		DasRegistrySql registry=(DasRegistrySql)ctx.getBean("registryBean");
    ActivationURLCreator activCreator = new ActivationURLCreator();
%>


<%@ include file="sangerheader.jsp" %>



<div id="main">

<%

String auto_id = request.getParameter("auto_id");
String action  = request.getParameter("action");

if ( action == null) 
	action = "display";

if ( auto_id == null)
	auto_id = "DS_410";


DasSource ds = register.getDasSource(auto_id);
DasClient[] clients = ActivationURLCreator.CONFIG_DAS_CLIENTS;

if ( ds == null ) {
	ds = new Das1Source();
}


String passkey   = request.getParameter("passkey");
if ( passkey == null ) 		 
    passkey ="";


%>
<h2>Edit AutoActivation URLs for DAS source <%=ds.getNickname()%></h2>
<p>
<a href="showdetails.jsp?auto_id=<%=ds.getId()%>">show details</a> for this DAS source. 
</p>
<%



int status = -1 ;


if ( ! passkey.equals(""))
      status = bean.validatePasskey(auto_id,passkey);

	  //out.print("status " + status);
	  
	  // status can be:
	  //  1: passkey o.k.
	  // -1: passkey wrong
	
if (coord.userOwnsASource(user,new DasSource[]{ds})) 
	status = 1;
	  
 if ( status != 1 ) {

       %>
       
	  <b>wrong key!</b>
	
	  	<%@ include file="editServerLogin.jsp" %> <%	
	  
			    
} else {
		  // we are logged in correctly. let the action begin
		  
		  if ( action.equals("remove")) {
			  out.print(" removing " + auto_id );
			  String activationurl =request.getParameter("activationurl"); 
			  out.print(" activationurl " + activationurl);

			  activCreator.removeActivationUrl(ds.getUrl(), activationurl);
		  }
	  
		  /// end of remove part
		  if ( action.equals("create")){
			  String client  = request.getParameter("client");
			  String url     = request.getParameter("url");
			  String dcs   = request.getParameter("coordSys");
			  
			int result = activCreator.createAutoURL(ds.getUrl(),client,url,dcs);
	
			if ( result == ActivationURLCreator.CREATION_OK ) {
				%><p><strong>Created the new auto-activation url.</strong></p><%
			} else if (result == ActivationURLCreator.NOT_A_URL) {
			 %>You did not provide a valid URL! Please correct this.<%
			} else {
			%>A problem occured while trying to save the new auto activation URL<%
	 		}
 		}
 %>
 <br/>
  <%@ include file="sangertablestart.jsp"%> 
		  
		  <tr id="row1"><td>ID:</td><td><%=auto_id%></td></tr>
		  <tr id="row2"><td>server nickname:</td><td><%=ds.getNickname()%></td></tr>
		  <tr id="row1"><td>server URL:</td><td><%=ds.getUrl()%></td></tr>
		  
  <%@ include file="sangertableend.jsp"%>   
		  <p>
		  You can use the following placeholders in the URL. They will automatically be
		  replaced with the corresponding data.
		  </p>
		  <%@ include file="sangertablestart.jsp"%> 
		  <table border="0">
		  <tr id="row0"><td>placeholder</td><td>will be replaced with</td></tr>
		  <tr id="row1"><td>%id</td><td> DAS source ID</td></tr>		  
		  <tr id="row2"><td>%testcode</td><td>the test code</td></tr>
		  <tr id="row1"><td>%nickname</td><td>nickname of DAS source</td></tr>
		  <tr id="row2"><td>%url</td><td>the URL of the DAS source</td></tr>
		  <tr id="row1"><td>%reference</td><td>if the testcode is a genomic region like 2:10000,20000 the %reference is <i>2</i></td></tr>
		  <tr id="row2"><td>%start</td><td>if the testcode is a genomic region like 2:10000,20000 the %start is <i>10000</i></td></tr>
		  <tr id="row1"><td>%stop</td><td>if the testcode is a genomic region like 2:10000,20000 the %stop is <i>20000</i></td></tr>  		 
  		  <tr id="row2"><td>%dsn</td><td>the DAS Source Name (dsn) on the das server. i.e. the bit after /das/ in the server url</td></tr>
		  </table>
		  <%@ include file="sangertableend.jsp"%> 
		 
		  <p>
		  
		  Add new Auto-Activation URL:
		  
		  </p>
		  
		  <form action="editAutoActivationUrl.jsp" method="POST">
	      <input type="hidden" name="passkey" value="<%=passkey %>"/>
		  <input type="hidden" name="auto_id" value="<%=ds.getId() %>"/>
		  <input type="hidden" name="action"  value="create" />
		  
		 
		  
		  DAS client:
		  <select name="client">
		  <%
		  			for (int i = 0; i < clients.length; i++) {
		  			DasClient client = clients[i];
		  %><option value="<%=client.getName()%>"
			  <% if ( i == 1) out.print(" selected "); %>
			  ><%=client.getName()%></option><%
		  }
		  %>
		  </select>
		  URL: <input type="text" name="url">
		
		  <select name="coordSys">
		  <%
		  		DasCoordinateSystem[] dcss = ds.getCoordinateSystem();

		  		for (int i = 0; i < dcss.length; i++) {
		  			DasCoordinateSystem das = dcss[i];
		  %><option value="<%=das %>"><%=das%></option><%
		  }
		  %>
		  </select>
		  
		  <input type="submit">
		  </form>
		  
		  	<p/>
			
		  
		  <form name="deleteAutoActivation" action="editAutoActivationUrl.jsp" method="POST">
	      <input type="hidden" name="action"  value="remove" />
	      <input type="hidden" name="passkey" value="<%=passkey %>"/>
		  <input type="hidden" name="auto_id" value="<%=ds.getId() %>"/>
		  <input type="hidden" name="activationurl" value="unk"/>
		
	<SCRIPT language="JavaScript">
	function submitform(activationUrl)
	{
	  document.deleteAutoActivation.activationurl.value = activationUrl;
	  document.deleteAutoActivation.submit();
	}
	</SCRIPT>
	
	manually configured activation URLs:
	 <table border="0">
		  <%
		  		AutoActivation[] myAas = activCreator.getUserConfigAutoActivation(ds.getUrl());

		  		for (int l = 0; l < myAas.length; l++) {
		  			AutoActivation aa = myAas[l];
		  			DasClient client = aa.getClient();
		  %>
				<tr><td>
				<div title="header=[delete link] body=[click here to remove this URL">
				<a href="javascript: submitform('<%=aa.getActivationURL()%>')"><img src="img/edit_remove.png" border="0" alt="remove" width="16" height="16"/></a>
				
				</div></td>
				<td><div title="header=[activate in das client] body=[folow this link to activate this DAS source in the DAS client <%=client.getName() %>]">
							
				<a href="<%=aa.getURL(ds)%>"><%
													if (client.getIconPath() != null) {
													out.print("<img src=\"" + client.getIconPath()
															+ "\" border=\"0\"/> ");
														}
											%></a><%=client.getName()%></div></td></tr>
				<tr><td>&nbsp;</td><td><%=aa.getActivationURL()%></td></tr><%
				}
				%>
		  </table>
		  
		  </form>		 		  
		 default  activation URLs:
		 <table border="0">
		  <%
		  		// all is fine in here

		  		AutoActivation[] aas = activCreator.getNotEditableConfigURLs(ds.getUrl());

		  		for (int l = 0; l < aas.length; l++) {
		  			AutoActivation aa = aas[l];
		  			DasClient client = aa.getClient();
		  %>
				<tr><td><div title="header=[activate in das client] body=[folow this link to activate this DAS source in the DAS client <%=client.getName() %>]">
				<a href="<%=aa.getURL(ds)%>"><%
						if (client.getIconPath() != null) {
						out.print("<img src=\"" + client.getIconPath()
								+ "\" border=\"0\"/> ");
							}
				%></a><%=client.getName()%> </div></td></tr>
				<tr><td>&nbsp;</td><td><%=aa.getActivationURL()%></td></tr>
				<%
				}
				%></table><%
				}
				%>
	




<%@ include file="sangerfooter.jsp" %>
<script src="boxover.js"></script>
  </body>
</html>